﻿/*
 *   BitFoxException.cs
 *   Copyright (c) 2011 - BitFox Software and Ricardo Federico Villafañes
 *   Contact: federvillaf@hotmail.com
 *   
 *   Licensing: This product is covered by Microsoft Reciprocal License (MS-RL)
 *              see https://bitfoxeval.codeplex.com/license
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;

namespace BitFox
{
    /// <summary>
    /// Exception used to indicate any problem with a <b>BitFox</b> component.
    /// </summary>
    [Serializable]
    public class BitFoxException : Exception
    {
        /// <summary>
        /// The error code that is encapsulated into this exception.
        /// </summary>
        public ErrorCode ErrorCode { get; private set; }

        /// <summary>
        /// Empty constructor.
        /// </summary>
        public BitFoxException() : this(ErrorCode.Unknown)
        {
        }

        /// <summary>
        /// Exception constructor.
        /// </summary>
        /// <param name="code">The code that describes the problem.</param>
        public BitFoxException(ErrorCode code)
        {
            this.ErrorCode = code;
        }

        /// <summary>
        /// Exception constructor.
        /// </summary>
        /// <param name="code">The code that describes the problem.</param>
        /// <param name="msg">An explanation message</param>
        public BitFoxException(ErrorCode code, string msg)
            : base(msg)
        {
            this.ErrorCode = code;
        }

        /// <summary>
        /// Exception constructor
        /// </summary>
        /// <param name="msg">The message that describes the problem</param>
        /// <remarks>The error code is set to Unknown</remarks>
        public BitFoxException(string msg)
            : base(msg)
        {
            this.ErrorCode = ErrorCode.Unknown;
        }

        /// <summary>
        /// Exception constructor
        /// </summary>
        /// <param name="msg">The message that describes the problem</param>
        /// <param name="exc">The inner exception caught</param>
        /// <remarks>The error code is set to Unknown</remarks>
        public BitFoxException(string msg, Exception exc)
            : base(msg, exc)
        {
            this.ErrorCode = ErrorCode.Unknown;
        }

        /// <summary>
        /// Exception constructor.
        /// </summary>
        /// <param name="code">The code that describes the problem.</param>
        /// <param name="inner">An inner exception caught.</param>
        public BitFoxException(ErrorCode code, Exception inner)
            : base(string.Empty, inner)
        {
            this.ErrorCode = code;
        }

        /// <summary>
        /// Exception constructor.
        /// </summary>
        /// <param name="code">The code that describes the problem.</param>
        /// <param name="msg">An explanation message for the problem.</param>
        /// <param name="inner">An inner exception caught</param>
        public BitFoxException(ErrorCode code, string msg, Exception inner)
            : base(msg, inner)
        {
            this.ErrorCode = code;
        }

        /// <summary>
        /// Protected constructor use for deserialize an exception of this type
        /// </summary>
        /// <param name="info">Info about serialized exception</param>
        /// <param name="ctx">Context under wich to deserialize</param>
        protected BitFoxException(SerializationInfo info, StreamingContext ctx)
            : base(info, ctx)
        {
            ErrorCode = (ErrorCode)info.GetValue("errorCode", typeof(ErrorCode));
        }

        /// <summary>
        /// Fill data that is needed to serialize
        /// </summary>
        /// <param name="info">The object in which to store the data to preserve.</param>
        /// <param name="context"></param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("errorCode", ErrorCode);
        }
    }
}
